/* 
 * ***** BEGIN LICENSE BLOCK ***** Version: NPL 1.1/GPL 2.0/LGPL 2.1 The
 * contents of this file are subject to the Netscape Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.mozilla.org/NPL/
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License. The
 * Original Code is lutsko.com code. The Initial Developer of the Original Code
 * is James Lutsko. Portions created by the Initial Developer are Copyright (C)
 * 2003 the Initial Developer. All Rights Reserved. Contributor(s):
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or the
 * GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
 * case the provisions of the GPL or the LGPL are applicable instead of those
 * above. If you wish to allow use of your version of this file only under the
 * terms of either the GPL or the LGPL, and not to allow others to use your
 * version of this file under the terms of the NPL, indicate your decision by
 * deleting the provisions above and replace them with the notice and other
 * provisions required by the GPL or the LGPL. If you do not delete the
 * provisions above, a recipient may use your version of this file under the
 * terms of any one of the NPL, the GPL or the LGPL. ***** END LICENSE BLOCK
 * *****
 */

package autoteam.tables;

import java.text.DateFormat;

import javax.swing.table.AbstractTableModel;

import autoteam.Child;

public class ChildEditorTableModel extends AbstractTableModel
{
    
    private static final long serialVersionUID = -4923513797409892767L;
    static String s = "Child";
    Child theChild;
    String vals[] = null;
    DateFormat format = null;
    
    static final String[] fieldNames = { "First Name", "Last Name", "Phone", "Email", "Comments", "Birthday" };
    
    public ChildEditorTableModel( Child c )
    {
	if ( vals == null )
	    vals = new String[fieldNames.length];
	format = DateFormat.getDateInstance( DateFormat.SHORT );
	setChild( c );
    }
    
    public void setChild( Child c )
    {
	theChild = c;
	vals[0] = theChild.firstName;
	vals[1] = theChild.lastName;
	vals[2] = theChild.phone;
	vals[3] = theChild.email;
	vals[4] = theChild.comment;
	vals[5] = format.format( theChild.birthDate );
    }
    
    public void transfer( ) throws java.text.ParseException
    {
	theChild.birthDate = format.parse( vals[5] );
	theChild.firstName = vals[0];
	theChild.lastName = vals[1];
	theChild.phone = vals[2];
	theChild.email = vals[3];
	theChild.comment = vals[4];
    }
    
    public int getColumnCount( )
    {
	return 2;
    }
    
    public int getRowCount( )
    {
	return fieldNames.length;
    }
    
    public String getColumnName( int col )
    {
	return (col == 0 ? "Child Property" : "Value");
    }
    
    public Object getValueAt( int row, int col )
    {
	if ( col == 0 )
	    return fieldNames[row];
	return vals[row];
    }
    
    /*
     * JTable uses this method to determine the default renderer/ editor for
     * each cell. If we didn't implement this method, then the last column would
     * contain text ("true"/"false"), rather than a check box.
     */
    // public Class getColumnClass(int c) {
    // return (c == 0 ? String.class : Object.class);
    // }
    
    /*
     * Don't need to implement this method unless your table's editable.
     */
    public boolean isCellEditable( int row, int col )
    {
	return (col == 1);
    }
    
    public void setValueAt( Object value, int row, int col )
    {
	vals[row] = (String) value;
    }
}
